<!DOCTYPE html><html lang="en" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>写算法题用到的一些常见的技巧、遇到的坑、模板 | Hexo</title><meta name="author" content="JoshuaLi"><meta name="copyright" content="JoshuaLi"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="写算法题用到的一些常见的技巧、遇到的坑、模板">
<meta property="og:type" content="article">
<meta property="og:title" content="写算法题用到的一些常见的技巧、遇到的坑、模板">
<meta property="og:url" content="http://joshua-li.gitee.io/joshuali/2023/06/09/%E5%86%99%E7%AE%97%E6%B3%95%E9%A2%98%E7%94%A8%E5%88%B0%E7%9A%84%E4%B8%80%E4%BA%9B%E5%B8%B8%E8%A7%81%E7%9A%84%E6%8A%80%E5%B7%A7%E3%80%81%E9%81%87%E5%88%B0%E7%9A%84%E5%9D%91%E3%80%81%E6%A8%A1%E6%9D%BF/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="写算法题用到的一些常见的技巧、遇到的坑、模板">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png">
<meta property="article:published_time" content="2023-06-09T13:46:07.000Z">
<meta property="article:modified_time" content="2023-06-13T06:09:41.789Z">
<meta property="article:author" content="JoshuaLi">
<meta property="article:tag" content="算法">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png"><link rel="shortcut icon" href="/joshuali/img/favicon.png"><link rel="canonical" href="http://joshua-li.gitee.io/joshuali/2023/06/09/%E5%86%99%E7%AE%97%E6%B3%95%E9%A2%98%E7%94%A8%E5%88%B0%E7%9A%84%E4%B8%80%E4%BA%9B%E5%B8%B8%E8%A7%81%E7%9A%84%E6%8A%80%E5%B7%A7%E3%80%81%E9%81%87%E5%88%B0%E7%9A%84%E5%9D%91%E3%80%81%E6%A8%A1%E6%9D%BF/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/joshuali/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
  root: '/joshuali/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: 'Copy successfully',
    error: 'Copy error',
    noSupport: 'The browser does not support'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  dateSuffix: {
    just: 'Just',
    min: 'minutes ago',
    hour: 'hours ago',
    day: 'days ago',
    month: 'months ago'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '写算法题用到的一些常见的技巧、遇到的坑、模板',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-06-13 14:09:41'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
    win.getCSS = (url,id = false) => new Promise((resolve, reject) => {
      const link = document.createElement('link')
      link.rel = 'stylesheet'
      link.href = url
      if (id) link.id = id
      link.onerror = reject
      link.onload = link.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        link.onload = link.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(link)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"><link rel="alternate" href="/joshuali/atom.xml" title="Hexo" type="application/atom+xml">
</head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/joshuali/archives/"><div class="headline">Articles</div><div class="length-num">3</div></a><a href="/joshuali/tags/"><div class="headline">Tags</div><div class="length-num">2</div></a><a href="/joshuali/categories/"><div class="headline">Categories</div><div class="length-num">1</div></a></div><hr class="custom-hr"/></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header"><nav id="nav"><span id="blog-info"><a href="/joshuali/" title="Hexo"><span class="site-name">Hexo</span></a></span><div id="menus"><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">写算法题用到的一些常见的技巧、遇到的坑、模板</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">Created</span><time class="post-meta-date-created" datetime="2023-06-09T13:46:07.000Z" title="Created 2023-06-09 21:46:07">2023-06-09</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">Updated</span><time class="post-meta-date-updated" datetime="2023-06-13T06:09:41.789Z" title="Updated 2023-06-13 14:09:41">2023-06-13</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/joshuali/categories/%E7%AC%94%E8%AE%B0/">笔记</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="写算法题用到的一些常见的技巧、遇到的坑、模板"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">Post View:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="写算法题用到的一些常见的技巧、遇到的坑、模板"><a href="#写算法题用到的一些常见的技巧、遇到的坑、模板" class="headerlink" title="写算法题用到的一些常见的技巧、遇到的坑、模板"></a>写算法题用到的一些常见的技巧、遇到的坑、模板</h1><h4 id="常见的技巧、遇到的坑"><a href="#常见的技巧、遇到的坑" class="headerlink" title="常见的技巧、遇到的坑"></a>常见的技巧、遇到的坑</h4><ol>
<li><p>提交时记得导包！！！</p>
</li>
<li><p>连乘、连加时int溢出</p>
</li>
<li><p>除0错误</p>
</li>
<li><p>数组内存没开够</p>
</li>
<li><p>注意看数范围，如果有累加或者相乘的，考虑是否会溢出int。</p>
</li>
<li><p>深搜或广搜时记得用用判重数组记录是否已遍历过</p>
</li>
<li><p>遇到题目强调数据无重复时，如1-n的排列或26个字母的排列，可用其当作数组下标存储相应信息</p>
</li>
<li><p>遇到要取一半或除2或除x时记得用在除之前转换成double</p>
</li>
<li><p>格式化输出System.out.printf(“%.4f”,res);注意不要写(“%0.4lf”,res);</p>
</li>
<li><p>定义静态全局变量时，不要在定义时初始化，要在函数中初始化，这样才能保证系统测试样例时每次都会初始化，不然只会初始化依次，如：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">static</span> ArrayList&lt;Character&gt; list;</span><br><span class="line"><span class="keyword">static</span> <span class="type">int</span> vis[];</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="type">int</span> <span class="title function_">function</span><span class="params">(String tiles)</span> &#123;</span><br><span class="line">	list = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line">	vis = <span class="keyword">new</span> <span class="title class_">int</span>[n];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>（原本a,b为int时），为防止溢出，转long类型时需这样写 res &#x3D; (long)a * b; 不能写成res &#x3D; (long)(a * b); 因为a和b相乘后已经溢出，再转换已经没用了 ，在运算前就要转long，使其有足够的位数进行运算</p>
</li>
<li><p>重写compare方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Arrays.sort(arr, <span class="keyword">new</span> <span class="title class_">Comparator</span>&lt;Integer&gt;() &#123;</span><br><span class="line">       <span class="comment">//重写compare方法</span></span><br><span class="line">        <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">compare</span><span class="params">(Integer a, Integer b)</span> &#123;</span><br><span class="line">             <span class="comment">//返回值&gt;0交换</span></span><br><span class="line">             <span class="keyword">return</span> b-a;</span><br><span class="line">        &#125;</span><br><span class="line"> &#125;);</span><br></pre></td></tr></table></figure>
</li>
<li><p>输入输出优化，记得抛出异常，最后记得写<strong>out.flush();</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Input_and_output</span> &#123;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">static</span> String s[];</span><br><span class="line">	<span class="keyword">static</span> <span class="type">PrintWriter</span> <span class="variable">out</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">PrintWriter</span>(<span class="keyword">new</span> <span class="title class_">BufferedWriter</span>(<span class="keyword">new</span> <span class="title class_">OutputStreamWriter</span>(System.out)));</span><br><span class="line">	<span class="keyword">static</span> <span class="type">BufferedReader</span> <span class="variable">in</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">BufferedReader</span>(<span class="keyword">new</span> <span class="title class_">InputStreamReader</span>(System.in));</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">		s = in.readLine().trim().split(<span class="string">&quot; &quot;</span>);</span><br><span class="line">		<span class="type">int</span> <span class="variable">a</span> <span class="operator">=</span> Integer.parseInt(s[<span class="number">0</span>]);</span><br><span class="line">		out.println(a);</span><br><span class="line">		</span><br><span class="line">		s = in.readLine().trim().split(<span class="string">&quot; &quot;</span>);</span><br><span class="line">		out.println(s[<span class="number">0</span>]);</span><br><span class="line">		</span><br><span class="line">		out.flush();</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ol>
<h4 id="常用模板"><a href="#常用模板" class="headerlink" title="常用模板"></a>常用模板</h4><ol>
<li>判断日期<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">bool</span> <span class="title">isLeap</span><span class="params">(<span class="type">int</span> y)</span></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> (y%<span class="number">4</span>==<span class="number">0</span>&amp;&amp;y%<span class="number">100</span>!=<span class="number">0</span>)||(y%<span class="number">400</span>==<span class="number">0</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">bool</span> <span class="title">check</span><span class="params">(<span class="type">int</span> year,<span class="type">int</span> month,<span class="type">int</span> day)</span></span>&#123;<span class="comment">//判断是否为合法日期</span></span><br><span class="line">    <span class="keyword">if</span>(month&gt;<span class="number">12</span>||month==<span class="number">0</span>) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    <span class="keyword">if</span>(day&gt;<span class="number">31</span>) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    <span class="keyword">if</span>(month==<span class="number">2</span>)&#123;</span><br><span class="line">        <span class="keyword">if</span>(<span class="built_in">isLeap</span>(year)&amp;&amp;day&gt;<span class="number">29</span>)</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        <span class="keyword">if</span>(!<span class="built_in">isLeap</span>(year)&amp;&amp;day&gt;<span class="number">28</span>)</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span>(month==<span class="number">4</span>||month==<span class="number">6</span>||month==<span class="number">9</span>||month==<span class="number">11</span>)&#123;</span><br><span class="line">        <span class="keyword">if</span>(day&gt;<span class="number">30</span>) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="type">int</span> n,i;</span><br><span class="line">    cin&gt;&gt;n;</span><br><span class="line">    <span class="type">int</span> a,b,c,d,e,f,g,h;<span class="comment">//8位数字</span></span><br><span class="line">    <span class="type">int</span> year,month,day;</span><br><span class="line">    <span class="type">bool</span> flag=<span class="literal">false</span>;</span><br><span class="line">    <span class="keyword">for</span>(i=n+<span class="number">1</span>;i&lt;=<span class="number">99999999</span>;i++)&#123;</span><br><span class="line">        year=i/<span class="number">10000</span>;</span><br><span class="line">        month=(i%<span class="number">10000</span>)/<span class="number">100</span>;</span><br><span class="line">        day=i%<span class="number">100</span>;</span><br><span class="line">        a=i%<span class="number">10</span>;</span><br><span class="line">        b=(i/<span class="number">10</span>)%<span class="number">10</span>;</span><br><span class="line">        c=(i/<span class="number">100</span>)%<span class="number">10</span>;</span><br><span class="line">        d=(i/<span class="number">1000</span>)%<span class="number">10</span>;</span><br><span class="line">        e=(i/<span class="number">10000</span>)%<span class="number">10</span>;</span><br><span class="line">        f=(i/<span class="number">100000</span>)%<span class="number">10</span>;</span><br><span class="line">        g=(i/<span class="number">1000000</span>)%<span class="number">10</span>;</span><br><span class="line">        h=(i/<span class="number">10000000</span>)%<span class="number">10</span>;</span><br><span class="line">        <span class="keyword">if</span>(a==h&amp;&amp;b==g&amp;&amp;c==f&amp;&amp;d==e&amp;&amp;flag==<span class="literal">false</span>)&#123;</span><br><span class="line">            <span class="keyword">if</span>(<span class="built_in">check</span>(year,month,day))&#123;</span><br><span class="line">                cout&lt;&lt;i&lt;&lt;endl;</span><br><span class="line">                flag=<span class="literal">true</span>;<span class="comment">//只输出一个回文</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span>(a==h&amp;&amp;b==g&amp;&amp;c==f&amp;&amp;d==e&amp;&amp;a==c&amp;&amp;b==d)&#123;</span><br><span class="line">            <span class="keyword">if</span>(<span class="built_in">check</span>(year,month,day))&#123;</span><br><span class="line">                cout&lt;&lt;i&lt;&lt;endl;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li>并查集<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">static</span> <span class="type">int</span> <span class="variable">N</span> <span class="operator">=</span> <span class="number">1000010</span>;</span><br><span class="line"><span class="keyword">static</span> <span class="type">int</span> pre[] = <span class="keyword">new</span> <span class="title class_">int</span>[N]; <span class="comment">//节点i的祖宗是pre[i]</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//初始化，下标从1开始，每个节点的祖先是自己</span></span><br><span class="line"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">init</span><span class="params">()</span>&#123;</span><br><span class="line">	<span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">1</span>; i &lt;= n; i++) &#123;</span><br><span class="line">		pre[i] = i;</span><br><span class="line">	&#125;	</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//找一个节点的祖宗节点</span></span><br><span class="line"><span class="keyword">static</span> <span class="type">int</span> <span class="title function_">find</span><span class="params">(<span class="type">int</span> x)</span> &#123;</span><br><span class="line">	<span class="keyword">if</span> (x == pre[x]) <span class="comment">//父节点等于自己，自己就是祖宗</span></span><br><span class="line">		<span class="keyword">return</span> x;</span><br><span class="line">	<span class="keyword">return</span> pre[x] = find(pre[x]);<span class="comment">//不断往上寻找父节点</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//合并两个节点</span></span><br><span class="line"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">union</span><span class="params">(<span class="type">int</span> a, <span class="type">int</span> b)</span> &#123;</span><br><span class="line">	a = find(a);</span><br><span class="line">	b = find(b);</span><br><span class="line">	<span class="comment">//判断两个节点的祖先是否相同，若不相同，则合并之</span></span><br><span class="line">	<span class="keyword">if</span> (a != b)</span><br><span class="line">		pre[a] = b;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<a target="_blank" rel="noopener" href="https://blog.csdn.net/the_ZED/article/details/105126583">参考资料</a></li>
</ol>
<p>例题 <a target="_blank" rel="noopener" href="https://www.acwing.com/problem/content/1251/">亲戚</a><br>题解：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.*;</span><br><span class="line"><span class="keyword">import</span> java.io.*;</span><br><span class="line"><span class="comment">//1:无需package</span></span><br><span class="line"><span class="comment">//2: 类名必须Main, 不可修改</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Main</span> &#123;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">static</span> <span class="type">int</span> <span class="variable">N</span> <span class="operator">=</span> <span class="number">1000010</span>;</span><br><span class="line">	<span class="keyword">static</span> <span class="type">int</span> pre[] = <span class="keyword">new</span> <span class="title class_">int</span>[N];</span><br><span class="line"></span><br><span class="line">	<span class="keyword">static</span> <span class="type">int</span> <span class="title function_">find</span><span class="params">(<span class="type">int</span> x)</span> &#123;</span><br><span class="line">		<span class="keyword">if</span> (x == pre[x])</span><br><span class="line">			<span class="keyword">return</span> x;</span><br><span class="line">		<span class="keyword">return</span> pre[x] = find(pre[x]);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">union</span><span class="params">(<span class="type">int</span> a, <span class="type">int</span> b)</span> &#123;</span><br><span class="line">		a = find(a);</span><br><span class="line">		b = find(b);</span><br><span class="line">		<span class="comment">//判断两个节点的祖先是否相同，若不相同，则合并之</span></span><br><span class="line">		<span class="keyword">if</span> (a != b)</span><br><span class="line">			pre[a] = b;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">static</span> String s[];</span><br><span class="line">	<span class="keyword">static</span> <span class="type">PrintWriter</span> <span class="variable">out</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">PrintWriter</span>(<span class="keyword">new</span> <span class="title class_">BufferedWriter</span>(<span class="keyword">new</span> <span class="title class_">OutputStreamWriter</span>(System.out)));</span><br><span class="line">	<span class="keyword">static</span> <span class="type">BufferedReader</span> <span class="variable">in</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">BufferedReader</span>(<span class="keyword">new</span> <span class="title class_">InputStreamReader</span>(System.in));</span><br><span class="line"></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException&#123;</span><br><span class="line">	    </span><br><span class="line">		s = in.readLine().trim().split(<span class="string">&quot; &quot;</span>);</span><br><span class="line">		<span class="type">int</span> <span class="variable">n</span> <span class="operator">=</span> Integer.parseInt(s[<span class="number">0</span>]);</span><br><span class="line">		<span class="type">int</span> <span class="variable">m</span> <span class="operator">=</span> Integer.parseInt(s[<span class="number">1</span>]);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//初始化，下标从1开始，每个人的祖先是自己</span></span><br><span class="line">		<span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">1</span>; i &lt;= n; i++) &#123;</span><br><span class="line">			pre[i] = i;</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; m; i++) &#123;</span><br><span class="line">    		s = in.readLine().trim().split(<span class="string">&quot; &quot;</span>);</span><br><span class="line">    		<span class="type">int</span> <span class="variable">a</span> <span class="operator">=</span> Integer.parseInt(s[<span class="number">0</span>]);</span><br><span class="line">    		<span class="type">int</span> <span class="variable">b</span> <span class="operator">=</span> Integer.parseInt(s[<span class="number">1</span>]);</span><br><span class="line">			<span class="keyword">if</span> (find(a) != find(b))</span><br><span class="line">				union(a, b);</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		s = in.readLine().trim().split(<span class="string">&quot; &quot;</span>);</span><br><span class="line">		<span class="type">int</span> <span class="variable">q</span> <span class="operator">=</span> Integer.parseInt(s[<span class="number">0</span>]);</span><br><span class="line">		<span class="type">int</span> <span class="variable">ans</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line">		<span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; q; i++) &#123;</span><br><span class="line">    		s = in.readLine().trim().split(<span class="string">&quot; &quot;</span>);</span><br><span class="line">    		<span class="type">int</span> <span class="variable">a</span> <span class="operator">=</span> Integer.parseInt(s[<span class="number">0</span>]);</span><br><span class="line">    		<span class="type">int</span> <span class="variable">b</span> <span class="operator">=</span> Integer.parseInt(s[<span class="number">1</span>]);</span><br><span class="line">			<span class="keyword">if</span> (find(a) == find(b))</span><br><span class="line">				out.println(<span class="string">&quot;Yes&quot;</span>);</span><br><span class="line">			<span class="keyword">else</span></span><br><span class="line">				out.println(<span class="string">&quot;No&quot;</span>);</span><br><span class="line">		&#125;</span><br><span class="line">		out.flush();</span><br><span class="line"></span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ol start="3">
<li>Dijkstra最短路</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Main</span> &#123;</span><br><span class="line">	<span class="keyword">static</span> <span class="keyword">final</span> <span class="type">int</span> <span class="variable">max</span> <span class="operator">=</span> <span class="number">10000</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line"><span class="comment">//		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));</span></span><br><span class="line">		<span class="type">Scanner</span> <span class="variable">sc</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Scanner</span>(System.in);</span><br><span class="line">		<span class="type">int</span> <span class="variable">n</span> <span class="operator">=</span> sc.nextInt();</span><br><span class="line">		<span class="type">int</span> <span class="variable">m</span> <span class="operator">=</span> sc.nextInt();</span><br><span class="line">		<span class="type">int</span>[][] matrix = <span class="keyword">new</span> <span class="title class_">int</span>[n+<span class="number">1</span>][n+<span class="number">1</span>];</span><br><span class="line"></span><br><span class="line">		<span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; matrix.length; i++) </span><br><span class="line">			<span class="keyword">for</span> (<span class="type">int</span> <span class="variable">j</span> <span class="operator">=</span> <span class="number">0</span>; j &lt; matrix.length; j++) </span><br><span class="line">				matrix[i][j] = max;</span><br><span class="line"></span><br><span class="line">		</span><br><span class="line">		<span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; m; i++) &#123;</span><br><span class="line">			<span class="type">int</span> <span class="variable">source</span> <span class="operator">=</span> sc.nextInt();</span><br><span class="line">			<span class="type">int</span> <span class="variable">target</span> <span class="operator">=</span> sc.nextInt();</span><br><span class="line">			<span class="type">int</span> <span class="variable">weight</span> <span class="operator">=</span> sc.nextInt();</span><br><span class="line">			<span class="keyword">if</span>(matrix[source][target] &gt; weight)</span><br><span class="line">				matrix[source][target] = weight;</span><br><span class="line">			<span class="keyword">if</span>(matrix[target][source] &gt; weight)</span><br><span class="line">				matrix[target][source] = weight;</span><br><span class="line">		&#125;</span><br><span class="line">			</span><br><span class="line">		dijkstra(matrix, <span class="number">1</span>);</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	 <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">dijkstra</span><span class="params">(<span class="type">int</span>[][] matrix,<span class="type">int</span> source)</span>&#123;</span><br><span class="line">		<span class="type">int</span>[] shortest = <span class="keyword">new</span> <span class="title class_">int</span>[matrix.length];</span><br><span class="line">		<span class="type">int</span>[] visied = <span class="keyword">new</span> <span class="title class_">int</span>[matrix.length];</span><br><span class="line">		String[] path = <span class="keyword">new</span> <span class="title class_">String</span>[matrix.length];</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; path.length; i++) &#123;</span><br><span class="line">			path[i] = <span class="keyword">new</span> <span class="title class_">String</span>(source +<span class="string">&quot;-&gt;&quot;</span> + i);</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		shortest[source] = <span class="number">0</span>;</span><br><span class="line">		visied[source] = <span class="number">1</span>;</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">1</span>; i &lt; matrix.length; i++) &#123;</span><br><span class="line">			<span class="type">int</span> <span class="variable">min</span> <span class="operator">=</span> Integer.MAX_VALUE , index = -<span class="number">1</span>;</span><br><span class="line">			</span><br><span class="line">			<span class="keyword">for</span> (<span class="type">int</span> <span class="variable">j</span> <span class="operator">=</span> <span class="number">0</span>; j &lt; matrix.length; j++) &#123;</span><br><span class="line">				<span class="keyword">if</span>(visied[j] == <span class="number">0</span> &amp;&amp; matrix[source][j] &lt; min) &#123;</span><br><span class="line">					min = matrix[source][j];</span><br><span class="line">					index = j;</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">			shortest[index] = min;</span><br><span class="line">			visied[index] = <span class="number">1</span>;</span><br><span class="line">			</span><br><span class="line">			<span class="keyword">for</span> (<span class="type">int</span> <span class="variable">j</span> <span class="operator">=</span> <span class="number">0</span>; j &lt; matrix.length; j++) &#123;</span><br><span class="line">				<span class="keyword">if</span>(visied[j] == <span class="number">0</span> &amp;&amp; matrix[source][j] &gt; matrix[source][index] + matrix[index][j] ) &#123;</span><br><span class="line">					matrix[source][j] = matrix[source][index] + matrix[index][j];</span><br><span class="line">					path[j] = path[index] + <span class="string">&quot;-&gt;&quot;</span> + j;</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">			</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line"><span class="comment">//		for (int i = 0; i &lt; matrix.length; i++) &#123;</span></span><br><span class="line"><span class="comment">//			if(shortest[i] != max)</span></span><br><span class="line"><span class="comment">//				System.out.println(path[i]+&quot;=&quot;+matrix[source][i]);</span></span><br><span class="line"><span class="comment">//		&#125;</span></span><br><span class="line">		System.out.println(shortest[matrix.length-<span class="number">1</span>]);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<ol start="4">
<li><p>数位DP<br>例题 <a target="_blank" rel="noopener" href="https://leetcode.cn/problems/count-of-integers/">统计整数数目</a></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Solution</span> &#123;</span><br><span class="line">    <span class="keyword">static</span> <span class="type">int</span> max,min , mem[][] , MOD = (<span class="type">int</span>)<span class="number">1e9</span>+<span class="number">7</span>;</span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">count</span><span class="params">(String num1, String num2, <span class="type">int</span> min_sum, <span class="type">int</span> max_sum)</span> &#123;</span><br><span class="line">        max = max_sum;min = min_sum;</span><br><span class="line"></span><br><span class="line">        <span class="type">int</span> nums1[] = <span class="keyword">new</span> <span class="title class_">int</span>[num1.length()];</span><br><span class="line">        <span class="type">int</span> nums2[] = <span class="keyword">new</span> <span class="title class_">int</span>[num2.length()];</span><br><span class="line">        <span class="type">int</span> <span class="variable">num1_sum</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span>(<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; num1.length(); i++)&#123;</span><br><span class="line">            nums1[i] = num1.charAt(i) - <span class="string">&#x27;0&#x27;</span>;</span><br><span class="line">            num1_sum += nums1[i];</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">for</span>(<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; num2.length(); i++)&#123;</span><br><span class="line">            nums2[i] = num2.charAt(i) - <span class="string">&#x27;0&#x27;</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        <span class="type">int</span> <span class="variable">t</span> <span class="operator">=</span> (num1_sum &gt;=min &amp;&amp; num1_sum &lt;= max) ? <span class="number">1</span>:<span class="number">0</span>;</span><br><span class="line">        <span class="keyword">return</span> (count(nums2) - count(nums1) + MOD + t) % MOD;<span class="comment">// 避免负数 (坑)</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//因为有两部分的dfs,两个部分的dfs不能共用一个mem数组，所以必须分别对每一个dfs用一个mem数组（坑）</span></span><br><span class="line">    <span class="type">int</span> <span class="title function_">count</span><span class="params">(<span class="type">int</span> nums[])</span>&#123;</span><br><span class="line">        mem = <span class="keyword">new</span> <span class="title class_">int</span>[<span class="number">30</span>][<span class="number">410</span>];</span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; <span class="number">30</span>; i++)</span><br><span class="line">            Arrays.fill(mem[i],-<span class="number">1</span>);</span><br><span class="line">        <span class="keyword">return</span> dfs(<span class="number">0</span>,<span class="number">0</span>,nums,<span class="literal">true</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="type">int</span> <span class="title function_">dfs</span><span class="params">(<span class="type">int</span> i , <span class="type">int</span> sum, <span class="type">int</span> nums[] , <span class="type">boolean</span> is_limit)</span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(sum &gt; max) <span class="keyword">return</span> <span class="number">0</span>; <span class="comment">// 提前结束</span></span><br><span class="line">        <span class="keyword">if</span>(i == nums.length) <span class="keyword">return</span> sum &gt;= min ? <span class="number">1</span> : <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">if</span>(!is_limit &amp;&amp; mem[i][sum] != -<span class="number">1</span>) <span class="keyword">return</span> mem[i][sum]; <span class="comment">//记忆化搜索</span></span><br><span class="line">        <span class="type">int</span> <span class="variable">res</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line">        <span class="type">int</span> <span class="variable">up</span> <span class="operator">=</span> is_limit ? nums[i] : <span class="number">9</span>; <span class="comment">//根据是否有限制决定上限</span></span><br><span class="line">        <span class="comment">//枚举每一位选什么，每一位都可以从0开始选择（即j=0）</span></span><br><span class="line">        <span class="keyword">for</span>(<span class="type">int</span> <span class="variable">j</span> <span class="operator">=</span> <span class="number">0</span>; j &lt;= up; j++)&#123;</span><br><span class="line">            res =  (res + dfs(i+<span class="number">1</span> , sum + j , nums , is_limit &amp;&amp; j == up)) % MOD ;</span><br><span class="line">        &#125; </span><br><span class="line">        <span class="keyword">if</span>(!is_limit) mem[i][sum] = res; <span class="comment">//不可省略，因为res由is_limit、i、sum三个维度决定</span></span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>分组背包<br>例题 <a target="_blank" rel="noopener" href="https://www.luogu.com.cn/problem/P1077">摆花</a></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> class 摆花 &#123;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">static</span> <span class="type">int</span> <span class="variable">MOD</span> <span class="operator">=</span> (<span class="type">int</span>)<span class="number">1e6</span>+<span class="number">7</span> , mem[][];</span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">		<span class="type">Scanner</span> <span class="variable">sc</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Scanner</span>(System.in);</span><br><span class="line">		<span class="type">int</span> <span class="variable">n</span> <span class="operator">=</span> sc.nextInt();</span><br><span class="line">		<span class="type">int</span> <span class="variable">m</span> <span class="operator">=</span> sc.nextInt();</span><br><span class="line">		<span class="type">int</span> nums[] = <span class="keyword">new</span> <span class="title class_">int</span>[n];</span><br><span class="line">		mem = <span class="keyword">new</span> <span class="title class_">int</span>[<span class="number">110</span>][<span class="number">110</span>];</span><br><span class="line">		<span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; mem.length; i++) &#123;</span><br><span class="line">			Arrays.fill(mem[i],-<span class="number">1</span>);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; n; i++) &#123;</span><br><span class="line">			nums[i] = sc.nextInt();</span><br><span class="line">		&#125;</span><br><span class="line">		System.out.println( dfs(n-<span class="number">1</span>, m, nums));</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/*</span></span><br><span class="line"><span class="comment">	 * 原问题：用前i种花，完成m个盆摆放</span></span><br><span class="line"><span class="comment">	 * 子问题：假设第i种花选了k盆， 则需要用前i-1种花，完成m-k个盘摆放</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="keyword">static</span> <span class="type">int</span> <span class="title function_">dfs</span><span class="params">(<span class="type">int</span> i, <span class="type">int</span> m ,<span class="type">int</span> nums[])</span> &#123;</span><br><span class="line">		<span class="keyword">if</span> (i &lt; <span class="number">0</span>) &#123;</span><br><span class="line">			<span class="keyword">return</span> m == <span class="number">0</span> ? <span class="number">1</span> : <span class="number">0</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span>(mem[i][m] != -<span class="number">1</span>) <span class="keyword">return</span> mem[i][m];</span><br><span class="line"></span><br><span class="line">		<span class="type">int</span> <span class="variable">res</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line">		<span class="comment">// 枚举每种花选多少盆</span></span><br><span class="line">		<span class="keyword">for</span> (<span class="type">int</span> <span class="variable">k</span> <span class="operator">=</span> <span class="number">0</span>; k &lt;= nums[i]; k++) &#123;</span><br><span class="line">			<span class="keyword">if</span> (k &gt; m)</span><br><span class="line">				<span class="keyword">break</span>;</span><br><span class="line">			res = (res + dfs(i - <span class="number">1</span>, m - k , nums)) % MOD;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span> mem[i][m] =  res;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>快速幂</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//快速幂</span></span><br><span class="line"><span class="keyword">static</span> <span class="type">long</span> <span class="title function_">quick_power</span><span class="params">(<span class="type">long</span> base , <span class="type">long</span> power)</span> &#123;</span><br><span class="line">	<span class="type">long</span> <span class="variable">res</span> <span class="operator">=</span> <span class="number">1</span>;</span><br><span class="line">	<span class="keyword">while</span>(power&gt;<span class="number">0</span>) &#123;</span><br><span class="line">		<span class="keyword">if</span>((power&amp;<span class="number">1</span>)==<span class="number">1</span>) res = res * base % MOD;</span><br><span class="line">		base = base*base % MOD;</span><br><span class="line">		power &gt;&gt;= <span class="number">1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> res%MOD;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>BFS</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">BFS</span> &#123;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">static</span> <span class="type">int</span> <span class="variable">N</span> <span class="operator">=</span> <span class="number">3010</span>;</span><br><span class="line">	<span class="keyword">static</span> <span class="type">char</span> a[][] = <span class="keyword">new</span> <span class="title class_">char</span>[N][N];</span><br><span class="line">	<span class="keyword">static</span> <span class="type">int</span> r,c;</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">static</span> <span class="type">int</span> <span class="title function_">bfs</span><span class="params">(Node start,Node end)</span> &#123;</span><br><span class="line">		<span class="type">int</span> dis[][] = <span class="keyword">new</span> <span class="title class_">int</span>[N][N];</span><br><span class="line">		</span><br><span class="line">		Queue&lt;Node&gt; q = <span class="keyword">new</span> <span class="title class_">LinkedList</span>&lt;Node&gt;();</span><br><span class="line">		q.offer(start);</span><br><span class="line">		<span class="type">int</span> dx[] = &#123;<span class="number">0</span>,<span class="number">0</span>,<span class="number">1</span>,-<span class="number">1</span>&#125;;</span><br><span class="line">		<span class="type">int</span> dy[] = &#123;<span class="number">1</span>,-<span class="number">1</span>,<span class="number">0</span>,<span class="number">0</span>&#125;;</span><br><span class="line">		<span class="keyword">while</span>(!q.isEmpty()) &#123;</span><br><span class="line">			<span class="type">Node</span> <span class="variable">t</span> <span class="operator">=</span> q.poll();</span><br><span class="line">			<span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; <span class="number">4</span>; i++) &#123;</span><br><span class="line">				<span class="type">int</span> <span class="variable">newx</span> <span class="operator">=</span> t.x + dx[i]; </span><br><span class="line">				<span class="type">int</span> <span class="variable">newy</span> <span class="operator">=</span> t.y + dy[i]; </span><br><span class="line">				<span class="keyword">if</span>(newx &gt;= r||newx &lt; <span class="number">0</span>||newy &gt;= c||newy &lt; <span class="number">0</span>) <span class="keyword">continue</span>;</span><br><span class="line">				<span class="keyword">if</span>(a[newx][newy] == <span class="string">&#x27;#&#x27;</span>) <span class="keyword">continue</span>;</span><br><span class="line">				<span class="keyword">if</span>(dis[newx][newy] != <span class="number">0</span>) <span class="keyword">continue</span>;</span><br><span class="line">				</span><br><span class="line"><span class="comment">//				System.out.println(&quot;(&quot;+newx+&quot;,&quot;+newy+&quot;)&quot;);</span></span><br><span class="line">				dis[newx][newy] = dis[t.x][t.y]+<span class="number">1</span>;</span><br><span class="line">				<span class="keyword">if</span>(newx==end.x &amp;&amp; newy==end.y)</span><br><span class="line">					<span class="keyword">return</span> dis[newx][newy];</span><br><span class="line">				<span class="type">Node</span> <span class="variable">newNode</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Node</span>(newx,newy);</span><br><span class="line">				q.offer(newNode);</span><br><span class="line">							</span><br><span class="line">			&#125;</span><br><span class="line">			</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">static</span> String s[];</span><br><span class="line">	<span class="keyword">static</span> <span class="type">BufferedReader</span> <span class="variable">br</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">BufferedReader</span>(<span class="keyword">new</span> <span class="title class_">InputStreamReader</span>(System.in));</span><br><span class="line"><span class="comment">//	static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out) ));</span></span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">		<span class="type">Scanner</span> <span class="variable">scanner</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Scanner</span>(System.in);</span><br><span class="line">		<span class="type">int</span> <span class="variable">m</span> <span class="operator">=</span> scanner.nextInt();</span><br><span class="line">		<span class="type">Node</span> <span class="variable">start</span> <span class="operator">=</span> <span class="literal">null</span>,end=<span class="literal">null</span>;</span><br><span class="line"></span><br><span class="line">		<span class="keyword">for</span> (<span class="type">int</span> <span class="variable">k</span> <span class="operator">=</span> <span class="number">0</span>; k &lt; m; k++) &#123;</span><br><span class="line">			r = scanner.nextInt();</span><br><span class="line">			c = scanner.nextInt();</span><br><span class="line">			</span><br><span class="line">			<span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; r; i++) &#123;</span><br><span class="line">				<span class="type">String</span> <span class="variable">str</span> <span class="operator">=</span> scanner.next();</span><br><span class="line">				<span class="keyword">for</span> (<span class="type">int</span> <span class="variable">j</span> <span class="operator">=</span> <span class="number">0</span>; j &lt; c; j++) &#123;</span><br><span class="line">					a[i][j] = str.charAt(j);</span><br><span class="line">					<span class="keyword">if</span>(a[i][j] == <span class="string">&#x27;S&#x27;</span>) start = <span class="keyword">new</span> <span class="title class_">Node</span>(i,j);</span><br><span class="line">					<span class="keyword">if</span>(a[i][j] == <span class="string">&#x27;E&#x27;</span>) end = <span class="keyword">new</span> <span class="title class_">Node</span>(i,j);</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">			</span><br><span class="line">			<span class="type">int</span> <span class="variable">dis</span> <span class="operator">=</span> bfs(start, end);</span><br><span class="line">			<span class="keyword">if</span>(dis == <span class="number">0</span>) System.out.println(<span class="string">&quot;oop!&quot;</span>);</span><br><span class="line">			<span class="keyword">else</span> System.out.println(dis); </span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Node</span>&#123;</span><br><span class="line">	<span class="type">int</span> x,y;</span><br><span class="line">	<span class="keyword">public</span> <span class="title function_">Node</span><span class="params">()</span> &#123;&#125;</span><br><span class="line">	<span class="keyword">public</span> <span class="title function_">Node</span><span class="params">(<span class="type">int</span> x, <span class="type">int</span> y)</span> &#123;</span><br><span class="line">		<span class="built_in">super</span>();</span><br><span class="line">		<span class="built_in">this</span>.x = x;</span><br><span class="line">		<span class="built_in">this</span>.y = y;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>DFS<br>例题 <a target="_blank" rel="noopener" href="https://leetcode.cn/problems/maximum-number-of-fish-in-a-grid/description/">网格图中鱼的最大数目</a></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Main</span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">class</span> <span class="title class_">Solution</span> &#123;</span><br><span class="line">		<span class="keyword">static</span> <span class="type">int</span> cnts[][] = <span class="keyword">new</span> <span class="title class_">int</span>[<span class="number">11</span>][<span class="number">11</span>], st[][], cnt = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">		<span class="keyword">public</span> <span class="type">int</span> <span class="title function_">findMaxFish</span><span class="params">(<span class="type">int</span>[][] grid)</span> &#123;</span><br><span class="line">			st = <span class="keyword">new</span> <span class="title class_">int</span>[<span class="number">11</span>][<span class="number">11</span>];</span><br><span class="line">			<span class="type">int</span> <span class="variable">max</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line">			<span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; grid.length; i++) &#123;</span><br><span class="line">				<span class="keyword">for</span> (<span class="type">int</span> <span class="variable">j</span> <span class="operator">=</span> <span class="number">0</span>; j &lt; grid[<span class="number">0</span>].length; j++) &#123;</span><br><span class="line">					cnt = <span class="number">0</span>;</span><br><span class="line">					dfs(i, j, grid);</span><br><span class="line">					cnts[i][j] = cnt;</span><br><span class="line">					System.out.println(cnt);</span><br><span class="line">					<span class="keyword">if</span> (cnts[i][j] &gt; max)</span><br><span class="line">						max = cnts[i][j];</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">return</span> max;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		<span class="keyword">void</span> <span class="title function_">dfs</span><span class="params">(<span class="type">int</span> r, <span class="type">int</span> c, <span class="type">int</span>[][] grid)</span> &#123;</span><br><span class="line">			<span class="keyword">if</span> (st[r][c] == <span class="number">1</span>)</span><br><span class="line">				<span class="keyword">return</span>;</span><br><span class="line">			<span class="keyword">if</span> (grid[r][c] == <span class="number">0</span>)</span><br><span class="line">				<span class="keyword">return</span>;</span><br><span class="line">			<span class="keyword">else</span> &#123;</span><br><span class="line">				st[r][c] = <span class="number">1</span>;<span class="comment">// 标记已访问过的节点</span></span><br><span class="line">				cnt += grid[r][c];</span><br><span class="line">				<span class="keyword">if</span> (c + <span class="number">1</span> &lt; grid[<span class="number">0</span>].length)</span><br><span class="line">					dfs(r, c + <span class="number">1</span>, grid);</span><br><span class="line">				<span class="keyword">if</span> (c &gt; <span class="number">0</span>)</span><br><span class="line">					dfs(r, c - <span class="number">1</span>, grid);</span><br><span class="line">				<span class="keyword">if</span> (r + <span class="number">1</span> &lt; grid.length)</span><br><span class="line">					dfs(r + <span class="number">1</span>, c, grid);</span><br><span class="line">				<span class="keyword">if</span> (r &gt; <span class="number">0</span>)</span><br><span class="line">					dfs(r - <span class="number">1</span>, c, grid);</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">return</span>;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p><a target="_blank" rel="noopener" href="https://www.acwing.com/activity/content/11/">其他模板</a></p>
</li>
</ol>
<h4 id="常用思想"><a href="#常用思想" class="headerlink" title="常用思想"></a>常用思想</h4><ol>
<li>贡献法<br> 一般用于模拟会超时或超内存，这时只需要考虑每次操作对答案有什么贡献、影响，只记录这些操作贡献、影响，从而节省时间内存</li>
<li>前缀和<br> 操作中有大量的重复计算</li>
<li>二分<br> 用于有序的序列中，一般在排序操作后可以想想会不会用到二分</li>
</ol>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">Author: </span><span class="post-copyright-info"><a href="http://joshua-li.gitee.io/joshuali">JoshuaLi</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">Link: </span><span class="post-copyright-info"><a href="http://joshua-li.gitee.io/joshuali/2023/06/09/%E5%86%99%E7%AE%97%E6%B3%95%E9%A2%98%E7%94%A8%E5%88%B0%E7%9A%84%E4%B8%80%E4%BA%9B%E5%B8%B8%E8%A7%81%E7%9A%84%E6%8A%80%E5%B7%A7%E3%80%81%E9%81%87%E5%88%B0%E7%9A%84%E5%9D%91%E3%80%81%E6%A8%A1%E6%9D%BF/">http://joshua-li.gitee.io/joshuali/2023/06/09/%E5%86%99%E7%AE%97%E6%B3%95%E9%A2%98%E7%94%A8%E5%88%B0%E7%9A%84%E4%B8%80%E4%BA%9B%E5%B8%B8%E8%A7%81%E7%9A%84%E6%8A%80%E5%B7%A7%E3%80%81%E9%81%87%E5%88%B0%E7%9A%84%E5%9D%91%E3%80%81%E6%A8%A1%E6%9D%BF/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">Copyright Notice: </span><span class="post-copyright-info">All articles in this blog are licensed under <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> unless stating additionally.</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/joshuali/tags/%E7%AE%97%E6%B3%95/">算法</a></div><div class="post_share"><div class="social-share" data-image="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/joshuali/2023/06/14/%E5%88%A9%E7%94%A8ChatGPT%E5%B8%AE%E5%8A%A9fork()%E5%87%BD%E6%95%B0%EF%BC%8C%E9%80%9A%E4%BF%97%E6%98%93%E6%87%82/" title="利用ChatGPT帮助理解fork()函数，通俗易懂"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">Previous Post</div><div class="prev_info">利用ChatGPT帮助理解fork()函数，通俗易懂</div></div></a></div><div class="next-post pull-right"><a href="/joshuali/2023/06/06/hello-world/" title="Hello World"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">Next Post</div><div class="next_info">Hello World</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png" onerror="this.onerror=null;this.src='/joshuali/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">JoshuaLi</div><div class="author-info__description">菜鸟写博客捏</div></div><div class="card-info-data site-data is-center"><a href="/joshuali/archives/"><div class="headline">Articles</div><div class="length-num">3</div></a><a href="/joshuali/tags/"><div class="headline">Tags</div><div class="length-num">2</div></a><a href="/joshuali/categories/"><div class="headline">Categories</div><div class="length-num">1</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>Announcement</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>Catalog</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%86%99%E7%AE%97%E6%B3%95%E9%A2%98%E7%94%A8%E5%88%B0%E7%9A%84%E4%B8%80%E4%BA%9B%E5%B8%B8%E8%A7%81%E7%9A%84%E6%8A%80%E5%B7%A7%E3%80%81%E9%81%87%E5%88%B0%E7%9A%84%E5%9D%91%E3%80%81%E6%A8%A1%E6%9D%BF"><span class="toc-number">1.</span> <span class="toc-text">写算法题用到的一些常见的技巧、遇到的坑、模板</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%B8%B8%E8%A7%81%E7%9A%84%E6%8A%80%E5%B7%A7%E3%80%81%E9%81%87%E5%88%B0%E7%9A%84%E5%9D%91"><span class="toc-number">1.0.0.1.</span> <span class="toc-text">常见的技巧、遇到的坑</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%B8%B8%E7%94%A8%E6%A8%A1%E6%9D%BF"><span class="toc-number">1.0.0.2.</span> <span class="toc-text">常用模板</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%B8%B8%E7%94%A8%E6%80%9D%E6%83%B3"><span class="toc-number">1.0.0.3.</span> <span class="toc-text">常用思想</span></a></li></ol></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>Recent Post</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/joshuali/2023/06/14/%E5%88%A9%E7%94%A8ChatGPT%E5%B8%AE%E5%8A%A9fork()%E5%87%BD%E6%95%B0%EF%BC%8C%E9%80%9A%E4%BF%97%E6%98%93%E6%87%82/" title="利用ChatGPT帮助理解fork()函数，通俗易懂">利用ChatGPT帮助理解fork()函数，通俗易懂</a><time datetime="2023-06-14T14:27:07.000Z" title="Created 2023-06-14 22:27:07">2023-06-14</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/joshuali/2023/06/09/%E5%86%99%E7%AE%97%E6%B3%95%E9%A2%98%E7%94%A8%E5%88%B0%E7%9A%84%E4%B8%80%E4%BA%9B%E5%B8%B8%E8%A7%81%E7%9A%84%E6%8A%80%E5%B7%A7%E3%80%81%E9%81%87%E5%88%B0%E7%9A%84%E5%9D%91%E3%80%81%E6%A8%A1%E6%9D%BF/" title="写算法题用到的一些常见的技巧、遇到的坑、模板">写算法题用到的一些常见的技巧、遇到的坑、模板</a><time datetime="2023-06-09T13:46:07.000Z" title="Created 2023-06-09 21:46:07">2023-06-09</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/joshuali/2023/06/06/hello-world/" title="Hello World">Hello World</a><time datetime="2023-06-06T15:42:09.274Z" title="Created 2023-06-06 23:42:09">2023-06-06</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2023 By JoshuaLi</div><div class="framework-info"><span>Framework </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>Theme </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="Read Mode"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="Switch Between Light And Dark Mode"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="Toggle between single-column and double-column"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="Setting"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="Table Of Contents"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="Back To Top"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/joshuali/js/utils.js"></script><script src="/joshuali/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.umd.min.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>